#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
using namespace std;

void test_list1()
{
	list<int> il1;
	list<int> il2(3, 1);
	list<int> il3(++il2.begin(), --il2.end());
	list<int>li4(il2);




	il2.push_back(2);
	il2.push_back(3);
	il2.push_back(4);
	il2.push_back(5);

	for (auto e : il2)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_list2()
{
	list<int> il1;
	il1.push_back(1);
	il1.push_back(2);
	il1.push_back(3);
	il1.push_back(4);
	il1.push_back(5);
	il1.push_back(6);

	//il1.insert(il1.begin() + 2);

	for (auto e : il1)
	{
		cout << e << " ";
	}
	cout << endl;

	int x = 0;
	cin >> x;
	auto iter = il1.begin();
	while (x--)
	{
		++iter;
	}
	il1.insert(iter, 100);

	for (auto e : il1)
	{
		cout << e << " ";
	}
	cout << endl << endl;

	int y = 0;
	cin >> y;
	iter = find(il1.begin(), il1.end(), y);
	if (iter != il1.end())
	{
		il1.erase(iter);
	}

	for (auto e : il1)
	{
		cout << e << " ";
	}
	cout << endl << endl;
}

void test_list3()
{
	list<int> il;
	il.push_back(1);
	il.push_back(2);
	il.push_back(3);
	il.push_back(4);
	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;


	cout << "il.resize(8, 10)" << endl;
	il.resize(8, 10);

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;

	cout << "il.resize(2)" << endl;
	il.resize(2);
	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;



	cout << "il.resize(5)" << endl;

	il.resize(5);
	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;

}

void test_list4()
{
	list<int> il;
	il.push_back(2);
	il.push_back(1);
	il.push_back(0);
	il.push_back(10);
	il.push_back(100);
	il.push_back(21);

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;

	//sort(il.begin(), il.end());
	//il.sort();

	//less<int> ls;
	/*greater<int> gt;
	il.sort(gt);*/
	il.sort(greater<int>());

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;

}

void test_op1()
{
	//srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	vector<int> v;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand() + i;
		lt1.push_back(e);
		v.push_back(e);
	}

	int begin1 = clock();
	// 排序
	sort(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

void test_op2()
{
	//srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand();
		lt1.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();
	// 拷贝vector
	vector<int> v(lt2.begin(), lt2.end());

	// 排序
	sort(v.begin(), v.end());

	// 拷贝回lt2
	lt2.assign(v.begin(), v.end());

	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

void test_list5()
{
	std::list<double> first, second;

	first.push_back(3.1);
	first.push_back(2.2);
	first.push_back(2.9);

	second.push_back(3.7);
	second.push_back(7.1);
	second.push_back(1.4);

	first.sort();
	for (auto e : first)
	{
		cout << e << " ";
	}
	cout << endl << endl;
	second.sort();
	for (auto e : second)
	{
		cout << e << " ";
	}
	cout << endl << endl;

	first.merge(second);
	for (auto e : first)
	{
		cout << e << " ";
	}
	cout << endl << endl;

	cout << "second size:" << second.size() << endl;
	// (second is now empty)
		
}

void test_list6()
{
	list<int> il;
	il.push_back(1);
	il.push_back(1);
	il.push_back(3);
	il.push_back(2);
	il.push_back(2);
	il.push_back(2);

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;

	il.remove(1);

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;
	/*il.unique();

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;*/
}

bool is_even_numbers(const int& val)
{
	return val % 2 == 0;
}

void test_list7()
{
	list<int> il;
	il.push_back(1);
	il.push_back(2);
	il.push_back(3);
	il.push_back(4);
	il.push_back(5);
	il.push_back(6);

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;




	il.remove_if(is_even_numbers);




	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;
}

void test_list8()
{
	list<int> il;
	il.push_back(1);
	il.push_back(2);
	il.push_back(3);
	il.push_back(4);
	il.push_back(5);
	il.push_back(6);

	cout << "il:";
	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;

	std::list<int> first;

	first.push_back(100);
	first.push_back(200);
	first.push_back(300);

	cout << "first:";
	for (auto e : first)
	{
		cout << e << " ";
	}
	cout << endl << endl;



	il.splice(il.end(),first);


	cout << "il:";

	for (auto e : il)
	{
		cout << e << " ";
	}
	cout << endl << endl;
	cout << "first:";

	for (auto e : first)
	{
		cout << e << " ";
	}
	cout << endl << endl;
}

#include "list.h"
#include <iostream>
//
//int main()
//{
//	xsc::test_list6();
//
//	//test_op2();
//
//	return 0;
//}


int main()
{

	int ar[] = { 0,1, 2, 3, 4,  5, 6, 7, 8, 9 };

	int n = sizeof(ar) / sizeof(int);

	list<int> mylist(ar, ar + n);

	list<int>::iterator pos = find(mylist.begin(), mylist.end(), 5);

	reverse(mylist.begin(), pos);

	reverse(pos, mylist.end());

	list<int>::const_reverse_iterator crit = mylist.crbegin();

	while (crit != mylist.crend())

	{

		cout << *crit << " ";

		++crit;

	}

	cout << endl;

}